<!--
 * @Descripttion: 
 * @version: @ 1.0.0
 * @Author: ZJZ
 * @Date: 2021-05-19 08:42:14
 * @LastEditors: 朱江洲
 * @LastEditTime: 2022-04-14 15:04:17
-->
<template>
  <div>
    <el-form
      :model="setting"
      ref="setting"
      :rules="setingRules"
      :label-position="labelPosition"
      label-width="80px"
      :inline="false"
      size="normal"
    >
      <el-form-item label="标识名" prop="name" v-if="seting.type !== 18">
        <el-input v-model="seting.name" style="width: 279px"></el-input>
      </el-form-item>
      <el-form-item label="标题" prop="title" v-if="seting.type == 18">
        <el-input v-model="seting.title" style="width: 279px"></el-input>
      </el-form-item>
      <el-form-item label="提示文字" v-if="seting.type !== 18">
        <el-input v-model="seting.inputTips" style="width: 279px"></el-input>
      </el-form-item>
      <el-form-item
        label="选项设置"
        v-if="seting.type == 15 || seting.type == 3"
      >
        <div
          class="sel"
          v-for="(item, index) in seting.labelOptions"
          :key="index"
          style="width: 279px"
        >
          <el-input v-model="item.label" style="width: 120px"></el-input>
          <i @click="addChe" class="el-icon-circle-plus-outline"></i>
          <i @click="reduceChe(index)" class="el-icon-remove-outline"></i>
        </div>
      </el-form-item>
      <el-form-item
        label="转换学员信息字段"
        v-if="seting.fieldType == '' && seting.type == 5"
      >
        <div
          class="sel"
          v-for="(item, index) in seting.labelOptions"
          :key="index"
        >
          <el-input v-model="item.label" style="width: 120px"></el-input>
          <i @click="Add" class="el-icon-circle-plus-outline"></i>
          <i @click="reduce(index)" class="el-icon-remove-outline"></i>
        </div>
      </el-form-item>
      <el-form-item
        label="展示方式"
        v-if="seting.type == 3 || seting.type == 15"
      >
        <el-select
          v-model="seting.exhibition"
          @change="ExhChange"
          style="width: 279px"
        >
          <el-option
            v-for="item in ShowOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="默认值" v-if="seting.type == 3">
        <el-select
          v-model="seting.choiceList"
          multiple
          placeholder="请选择"
          v-if="seting.exhibition == 1"
          style="width: 279px"
        >
          <el-option
            v-for="item in seting.labelOptions"
            :key="item.value"
            :label="item.label"
            :value="item.label"
          >
          </el-option>
        </el-select>
        <el-select
          v-model="seting.choiceList"
          multiple
          placeholder="请选择"
          v-else
          style="width: 279px"
        >
          <el-option
            v-for="item in seting.labelOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="默认值" v-if="seting.type == 15">
        <el-select
          v-model="seting.radio"
          placeholder="请选择"
          style="width: 279px"
        >
          <el-option
            v-for="item in seting.labelOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="默认值" v-if="seting.type == 11">
        <el-date-picker
          :placeholder="seting.inputTips"
          type="datetime"
          value-format="yyyy-MM-dd HH:mm:ss"
          v-model="seting.dates"
          style="width: 279px"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="默认值" v-if="seting.type == 14">
        <div class="dateP">
          <el-date-picker
            v-model="seting.date"
            type="datetimerange"
            value-format="yyyy-MM-dd HH:mm:ss"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            style="width: 279px"
          >
          </el-date-picker>
        </div>
      </el-form-item>
      <el-form-item label="编号规则" v-if="seting.type == 16">
        <el-button type="primary" size="default" @click="toConfigure"
          >点击配置</el-button
        >
      </el-form-item>
      <el-form-item label="编号说明" v-if="seting.type == 16">
        <span>自动计数、起始编号、递增数</span>
      </el-form-item>
      <el-form-item label="是否允许手动填写编号" v-if="seting.type == 16">
        <el-checkbox v-model="seting.autoNumber"> 允许 </el-checkbox>
      </el-form-item>
      <el-form-item label="是否必填" v-if="seting.type !== 18">
        <el-checkbox v-model="seting.isNull"> 必填 </el-checkbox>
      </el-form-item>

      <el-form-item label="是否启用">
        <el-checkbox v-model="seting.isHidden"> 启用 </el-checkbox>
      </el-form-item>
    </el-form>
    <el-dialog title="设置编号规则" :visible.sync="fieldRuleShow" width="50%">
      <div class="Examples">编号示例 {{ rulesStr }}</div>
      <draggable
        tag="el-collapse"
        class="dragRules"
        :list="seting.numberRule"
        group="comp"
        style="width: 100%"
      >
        <div
          v-for="(item, index) in seting.numberRule"
          :key="index"
          class="numRule"
        >
          <div v-if="item.type == 1">
            <el-input
              placeholder="自动计数"
              disabled
              style="width: 180px"
            ></el-input>
            <el-input
              v-model="item.start"
              placeholder="起始编号"
              type="number"
              style="width: 180px; margin: 0 20px"
            ></el-input>
            <el-input
              v-model="item.increase"
              placeholder="递增数"
              type="number"
              style="width: 180px"
            ></el-input>
          </div>
          <div v-if="item.type == 2">
            <el-input
              placeholder="固定字符"
              style="width: 180px"
              disabled
            ></el-input>
            <el-input
              v-model="item.fixedChar"
              placeholder="请输入固定字符"
              style="width: 180px; margin-left: 20px"
            ></el-input>
          </div>
          <i
            class="iconfont icon-shanchu"
            v-if="item.type !== 1"
            @click="delConfigure(index)"
            style="cursor: pointer"
          ></i>
        </div>
      </draggable>
      <div>
        <el-button type="text" size="default" @click="addRules">
          + 新增编号规则
        </el-button>
      </div>
      <span slot="footer">
        <el-button @click="fieldRuleShow = false">取消</el-button>
        <el-button type="primary" @click="fieldRuleShow = false">
          确定
        </el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import draggable from "vuedraggable";
export default {
  props: {
    seting: {
      type: Object,
      required: true,
    },
  },
  components: {
    draggable,
  },
  data() {
    return {
      fieldRuleShow: false,
      rulesStr: "",
      ShowOptions: [
        {
          value: 1,
          label: "平铺",
        },
        {
          value: 2,
          label: "下拉",
        },
      ],
      setting: {},
      option: [],
      labelPosition: "top",
      setingRules: {
        name: [{ required: true, message: "请输入活动名称", trigger: "blur" }],
        title: [{ required: true, message: "请输入标题名称", trigger: "blur" }],
      },
    };
  },
  watch: {
    seting: {
      deep: true,
      handler(val) {
        if (val) {
          console.log(val);
          if (val.type == 1) {
            console.log(val.rules.text);
            if (val.isNull == true) {
              val.rules.text.map((item) => {
                item.required = true;
              });
            } else {
              val.rules.text.map((item) => {
                item.required = false;
              });
            }
          } else if (val.type == 2) {
            if (val.isNull == true) {
              val.rules.rowText.map((item) => {
                item.required = true;
              });
            } else {
              val.rules.rowText.map((item) => {
                item.required = false;
              });
            }
          } else if (val.type == 3) {
            if (val.isNull == true) {
              val.rules.choiceList.map((item) => {
                item.required = true;
              });
            } else {
              val.rules.choiceList.map((item) => {
                item.required = false;
              });
            }
          } else if (val.type == 15) {
            if (val.isNull == true) {
              val.rules.radio.map((item) => {
                item.required = true;
              });
            } else {
              val.rules.radio.map((item) => {
                item.required = false;
              });
            }
          } else if (val.type == 14) {
            if (val.isNull == true) {
              val.rules.date.map((item) => {
                item.required = true;
              });
            } else {
              val.rules.date.map((item) => {
                item.required = false;
              });
            }
          } else if (val.type == 12) {
            if (val.isNull == true) {
              val.rules.dropDownPer.map((item) => {
                item.required = true;
              });
            } else {
              val.rules.dropDownPer.map((item) => {
                item.required = false;
              });
            }
          } else if (val.type == 13) {
            console.log(val);
            if (val.isNull == true) {
              val.rules.dropDownDet.map((item) => {
                item.required = true;
              });
            } else {
              val.rules.dropDownDet.map((item) => {
                item.required = false;
              });
            }
          } else if (val.type == 9) {
            if (val.isNull == true) {
              val.rules.fileImg.map((item) => {
                item.required = true;
              });
            } else {
              val.rules.fileImg.map((item) => {
                item.required = false;
              });
            }
          } else if (val.type == 10) {
            if (val.isNull == true) {
              val.rules.fileList1.map((item) => {
                item.required = true;
              });
            } else {
              val.rules.fileList1.map((item) => {
                item.required = false;
              });
            }
          } else if (val.type == 11) {
            if (val.isNull == true) {
              val.rules.dates.map((item) => {
                item.required = true;
              });
            } else {
              val.rules.dates.map((item) => {
                item.required = false;
              });
            }
          } else if (val.type == 16) {
            if (val.isNull == true) {
              val.rules.cusNum.map((item) => {
                item.required = true;
              });
            } else {
              val.rules.cusNum.map((item) => {
                item.required = false;
              });
            }
            this.rulesStr = "";
            val.numberRule.forEach((item) => {
              if (item.type == 1) {
                this.rulesStr += item.start + "-";
              } else if (item.type == 2) {
                this.rulesStr += item.fixedChar + "-";
              }
            });
            this.rulesStr = this.rulesStr.substr(0, this.rulesStr.length - 1);
            console.log(this.rulesStr);
          }
          this.setting = val;
        }
      },
    },
  },
  methods: {
    // 新增规则
    addRules() {
      this.seting.numberRule.push({
        type: 2,
        fixedChar: "",
      });
    },
    // 删除规则
    delConfigure(i) {
      this.seting.numberRule.splice(i, 1);
    },
    toConfigure() {
      this.fieldRuleShow = true;
    },
    ExhChange() {
      if (this.seting.type == 3) {
        this.seting.choiceList = [];
      }
    },
    Add() {
      if (this.seting.disab == 0) return;
      this.seting.labelOptions.push({
        value: this.seting.labelOptions.length - 0 + 1,
        label: "选项" + (this.seting.labelOptions.length - 0 + 1),
      });
    },
    reduce(i) {
      if (this.seting.disab == 0) return;
      if (this.seting.labelOptions.length > 1) {
        this.seting.labelOptions.splice(i, 1);
      }
    },
    addChe() {
      this.seting.labelOptions.push({
        value: this.seting.labelOptions.length - 0 + 1,
        label: "选项" + (this.seting.labelOptions.length - 0 + 1),
      });
    },
    reduceChe(i) {
      if (this.seting.labelOptions.length > 1) {
        this.seting.labelOptions.splice(i, 1);
      }
    },
  },
};
</script>

<style lang="less" scoped>
/deep/ .el-picker-panel {
  right: 100px !important;
}
/deep/ .el-form-item__label {
  line-height: 20px;
}
.sel {
  display: flex;
  margin-top: 10px;
  i {
    font-size: 25px;
    margin-top: 8px;
    margin-left: 10px;
  }
}

/deep/ .el-time-panel {
  margin-left: 0.5rem !important;
  left: -50px !important;
}

.numRule {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.numRule:nth-child(n + 2) {
  margin-top: 20px;
}
.dragRules {
  height: 600px;
  overflow-y: auto;
  border: none;
  border-bottom: 1px solid #ececec;
}
.Examples {
  margin-bottom: 10px;
}
</style>